/**
 * 
 */
package com.xbang.attendance.web.action.warehouse.stock;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.system.Region;
import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.service.InventoryService;
import com.xbang.attendance.service.RegionService;
import com.xbang.attendance.service.SystemService;
import com.xbang.attendance.service.WarehouseService;
import com.xbang.attendance.utils.FieldUtil;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 2886204759816132683L;

	/** 库存列表 */
	private List<Inventory> inventorys = new ArrayList<Inventory>();

	private List<Warehouse> warehouses = new ArrayList<Warehouse>();

	private Warehouse warehouse;
	
	@Autowired
	private WarehouseService warehouseService;

	@Autowired
	private SystemService systemService;

	@Autowired
	private InventoryService inventoryService;
	@Autowired
	private RegionService regionService;
	private List<Region> regions = new ArrayList<Region>();

	private Region region;

	private Integer id;

	private Integer warehouseId;
	private Integer pid;
	private String doNo;

	private Integer year;

	private Integer month;

	private List<String> timeparts = new ArrayList<String>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {

		inventorys = systemService.getObjects(Inventory.class);

		return super.execute();
	}

	@Action("/warehouse/stock/list/search")
	public String searchWarehouse() throws Exception {

		regions = regionService.getAKZRegion();

		// warehouses = inventoryService.getObjects(Warehouse.class);

		return "list-search";
	}

	@Action("/warehouse/stock/region/${id}/view")
	public String chooseWarehouse() throws Exception {

		regions = regionService.getAKZRegion();

		if (id != null) {
			region = (Region) inventoryService.getObject(Region.class, id);

			String hqlSentence = "FROM Warehouse warehouse WHERE warehouse.region.id = :id";

			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("id", id);

			warehouses = inventoryService.searchForList(hqlSentence, parameters);
		}

		return "list-search";
	}

	@Action("/warehouse/stock/waresearch/${warehouseId}/view")
	public String viewWarehouseStock() throws Exception {

		if (warehouseId != null) {
			searchComponent.resetGroupSentence();
			searchComponent.resetOrderSentence();
			searchComponent.resetWhereSentence();
			searchComponent.addSearchElement("eq.inventory.warehouse.id", warehouseId);
			warehouse = (Warehouse) inventoryService.get(Warehouse.class, warehouseId);
		} else {
			return "r:/warehouse/stock/list/search";
		}

		warehouses = inventoryService.getObjects(Warehouse.class);

		String hqlSentence = "FROM Inventory inventory WHERE 1 = 1 ";

		Result result = inventoryService.searchByPage(hqlSentence, searchComponent, page);
		inventorys = result.getItems();
		page = result.getPage();

		return "index";
	}

	@Action("/warehouse/stock/waresearch")
	public String searhcWarehou() throws Exception {
		if (warehouse != null) {
			searchComponent.resetGroupSentence();
			searchComponent.resetOrderSentence();
			searchComponent.resetWhereSentence();
			searchComponent.addSearchElement("eq.inventory.warehouse.code", warehouse.getCode());
		} else {
			return "r:/warehouse/stock/list/search";
		}

		String hqlSentence = "FROM Warehouse warehouse WHERE 1 = 1 ";
		String parameterKey = "eqInventoryWarehouseCode";

		warehouses = inventoryService.getObjects(Warehouse.class);

		Object value = getSearchParameterValue(parameterKey);
		if (value != null) {
			SearchComponent tmpSearchComponent = new SearchComponent();
			tmpSearchComponent.getParameters().put("eqWarehouseCode", value);
			tmpSearchComponent.getWhereSentences().put("eq.warehouse.code", " AND (warehouse.code = :eqWarehouseCode)");

			List objects = inventoryService.searchForList(hqlSentence, tmpSearchComponent);
			if (objects != null && !objects.isEmpty()) {
				warehouse = (Warehouse) objects.get(0);
			}
		}

		hqlSentence = "FROM Inventory inventory WHERE 1 = 1 ";

		Result result = inventoryService.searchByPage(hqlSentence, searchComponent, page);
		inventorys = result.getItems();
		page = result.getPage();

		return "index";

	}

	@Action("/warehouse/stock/getwarehouse")
	public String getInventoryByWarehouse() {
		List jsons = new ArrayList();
		HttpServletResponse response = ServletActionContext.getResponse();
		//ServletActionContext.getRequest().getRequestDispatcher(arg0)
		try {
			this.inventorys = inventoryService.getInventoryByWarehouseId(warehouseId);
			for (Inventory inventory : inventorys) {
				Map mp = new HashMap();
				mp.put("id", inventory.getId());
				mp.put("pcode", inventory.getProduct().getProductCode());
				mp.put("amount", inventory.getAmount());
				mp.put("pid", pid);
				mp.put("doNo", doNo);
				mp.put("warehouseId", warehouseId);
				jsons.add(mp);
			}
		} catch (Exception e) {
			setErrorMessage(e.getMessage());
		}
		JSONArray text = JSONArray.fromObject(jsons);
		try {
			response.getWriter().print(text);
		} catch (IOException e) {
			setErrorMessage(e.getMessage());
		}
		return null;
	}
//
//	@Action("/warehouse/stock/export")
//	public String export() throws Exception {
//		String exportFile =null;
//		String webPath =ServletActionContext.getRequest().getRealPath("//download//warehouse//stock");
//		HttpServletRequest request = ServletActionContext.getRequest();
//		String urlPath = request.getRequestURL().toString();
//		String contextPath = request.getContextPath();
//		String contextURL = urlPath.split(contextPath)[0] + contextPath;
//		exportFile = contextURL + "/download/warehouse/stock/" + warehouseService.genericExcelFile(webPath,warehouseId);
//		return "r:" + exportFile;
//	}
	@Action("/modify/stock/export")
	public String export() throws Exception{
		try{
			String exportFile = "";
			String webPath = ServletActionContext.getRequest().getRealPath("//download//warehouse//product");
			HttpServletRequest request = ServletActionContext.getRequest();
			String urlPath = request.getRequestURL().toString();
			String contextPath = request.getContextPath();
			String contextURL = urlPath.split(contextPath)[0] + contextPath;
			exportFile = contextURL + "/download/warehouse/product/" + inventoryService.genericExcelFile(webPath,null);
			//System.out.println(exportFile);
			
			return "r:" + exportFile;
		}catch (Exception e) {
			e.printStackTrace();
			logger.info(e);
			return ERROR;
		}
	}
	
	
	/**
	 * @return the inventorys
	 */
	public List<Inventory> getInventorys() {
		return inventorys;
	}

	/**
	 * @param inventorys
	 *            the inventorys to set
	 */
	public void setInventorys(List<Inventory> inventorys) {
		this.inventorys = inventorys;
	}

	@Override
	public void resetCustomerSearchCriteria() {
		// TODO Auto-generated method stub

	}

	public SystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(SystemService systemService) {
		this.systemService = systemService;
	}

	public InventoryService getInventoryService() {
		return inventoryService;
	}

	public void setInventoryService(InventoryService inventoryService) {
		this.inventoryService = inventoryService;
	}

	public Integer getWarehouseId() {
		return warehouseId;
	}

	public void setWarehouseId(Integer warehouseId) {
		this.warehouseId = warehouseId;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public String getDoNo() {
		return doNo;
	}

	public void setDoNo(String doNo) {
		this.doNo = doNo;
	}

	/**
	 * @return the warehouses
	 */
	public List<Warehouse> getWarehouses() {
		return warehouses;
	}

	/**
	 * @param warehouses
	 *            the warehouses to set
	 */
	public void setWarehouses(List<Warehouse> warehouses) {
		this.warehouses = warehouses;
	}

	/**
	 * @return the warehouse
	 */
	public Warehouse getWarehouse() {
		return warehouse;
	}

	/**
	 * @param warehouse
	 *            the warehouse to set
	 */
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}

	/**
	 * @return the regions
	 */
	public List<Region> getRegions() {
		return regions;
	}

	/**
	 * @param regions
	 *            the regions to set
	 */
	public void setRegions(List<Region> regions) {
		this.regions = regions;
	}

	/**
	 * @return the region
	 */
	public Region getRegion() {
		return region;
	}

	/**
	 * @param region
	 *            the region to set
	 */
	public void setRegion(Region region) {
		this.region = region;
	}

	/**
	 * @return the id
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the timeparts
	 */
	public List<String> getTimeparts() {
		return timeparts;
	}

	/**
	 * @param timeparts
	 *            the timeparts to set
	 */
	public void setTimeparts(List<String> timeparts) {
		this.timeparts = timeparts;
	}

	/**
	 * @return the year
	 */
	public Integer getYear() {
		return year;
	}

	/**
	 * @param year
	 *            the year to set
	 */
	public void setYear(Integer year) {
		this.year = year;
	}

	/**
	 * @return the month
	 */
	public Integer getMonth() {
		return month;
	}

	/**
	 * @param month
	 *            the month to set
	 */
	public void setMonth(Integer month) {
		this.month = month;
	}

}
